package com.amarsoft.task.security.login;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Spring;
import javax.swing.SpringLayout;

public class AccountPasswordEnterWindow extends JDialog
{
  private static final long serialVersionUID = 1L;
  private String account;
  private String password;
  private boolean enterOk = false;
  private JButton cancelButton;
  private JLabel errorLabel;
  private JTextField accountTextField;
  private JLabel accountLabel;
  private JLabel passwordLabel;
  private JButton okButton;
  private JPasswordField passwordPasswordField;

  public AccountPasswordEnterWindow()
  {
    initComponents();
    fitPosition();
  }

  public AccountPasswordEnterWindow(Frame owner) {
    super(owner);
    initComponents();
    fitPosition();
  }

  public AccountPasswordEnterWindow(Dialog owner) {
    super(owner);
    initComponents();
    fitPosition();
  }

  public void setAccountLabel(String label)
  {
    this.accountLabel.setText(label + ":");
  }

  public void setPasswordLabel(String label)
  {
    this.passwordLabel.setText(label + ":");
  }

  private void fitPosition()
  {
    Container c = getParent();
    Point center = null;
    if ((c != null) && (c.isVisible())) {
      center = new Point(c.getX() + c.getWidth() / 2, c.getY() + c.getHeight() / 2);
    } else {
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      center = new Point((int)d.getWidth() / 2, (int)d.getHeight() / 2);
    }
    Point o = new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2));
    setLocation(o);
  }

  private void initComponents()
  {
    setDefaultCloseOperation(0);
    setTitle("Login To System");
    setModal(true);
    setName("loginwindow");

    setResizable(false);

    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    c.add(Box.createVerticalStrut(20), "North");
    c.add(Box.createHorizontalStrut(20), "West");
    c.add(Box.createHorizontalStrut(20), "East");
    c.add(getInputPanel(), "Center");
    c.add(getOptionPanel(), "South");

    pack();
    setSize(300, 180);
  }

  private JPanel getInputPanel() {
    JPanel ipp = new JPanel();
    SpringLayout l = new SpringLayout();
    ipp.setLayout(l);

    JLabel[] label = { getAccountLabel(), getPasswordLabel() };

    Component[] comp = { getAccountTextField(), getPasswordPasswordField() };

    for (int i = 0; i < label.length; ++i) {
      ipp.add(label[i]);
      ipp.add(comp[i]);

      if (i == 0) {
        l.putConstraint("North", label[i], Spring.constant(20), "North", ipp);
        l.putConstraint("West", label[i], 20, "West", ipp);
      } else {
        l.putConstraint("North", label[i], Spring.constant(20), "South", label[(i - 1)]);
        l.putConstraint("East", label[i], 0, "East", label[(i - 1)]);
      }

      l.putConstraint("West", comp[i], Spring.constant(5), "East", label[i]);
      l.putConstraint("South", comp[i], 0, "South", label[i]);
    }
    ipp.add(getErrorLabel());
    l.putConstraint("North", getErrorLabel(), Spring.constant(20), "South", label[(label.length - 1)]);
    l.putConstraint("West", getErrorLabel(), 0, "West", label[(label.length - 1)]);
    return ipp;
  }

  private JLabel getAccountLabel()
  {
    if (this.accountLabel == null) {
      this.accountLabel = new JLabel("Account:");
    }
    return this.accountLabel;
  }

  private JLabel getPasswordLabel() {
    if (this.passwordLabel == null) {
      this.passwordLabel = new JLabel("Password:");
    }
    return this.passwordLabel;
  }

  private JLabel getErrorLabel() {
    if (this.errorLabel == null) {
      this.errorLabel = new JLabel("");
      this.errorLabel.setForeground(new Color(255, 0, 0));
      this.errorLabel.setAutoscrolls(true);
    }
    return this.errorLabel;
  }

  private JTextField getAccountTextField() {
    if (this.accountTextField == null) {
      this.accountTextField = new JTextField();
      this.accountTextField.setPreferredSize(new Dimension(160, 24));

      this.accountTextField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          AccountPasswordEnterWindow.this.passwordPasswordField.requestFocusInWindow();
        }
      });
      this.accountTextField.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent evt) {
          AccountPasswordEnterWindow.this.errorLabel.setText("");
        }
      });
    }
    return this.accountTextField;
  }

  private JPasswordField getPasswordPasswordField() {
    if (this.passwordPasswordField == null) {
      this.passwordPasswordField = new JPasswordField();
      this.passwordPasswordField.setPreferredSize(new Dimension(160, 24));

      this.passwordPasswordField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          AccountPasswordEnterWindow.this.okButton.requestFocusInWindow();
        }
      });
      this.passwordPasswordField.addFocusListener(new FocusAdapter() {
        public void focusGained(FocusEvent evt) {
          AccountPasswordEnterWindow.this.errorLabel.setText("");
        }
      });
    }
    return this.passwordPasswordField;
  }

  private JPanel getOptionPanel() {
    JPanel opp = new JPanel(new FlowLayout());
    opp.add(getOkButton());
    opp.add(getCancelButton());
    return opp;
  }

  private JButton getOkButton() {
    if (this.okButton == null) {
      this.okButton = new JButton("Ok");
      this.okButton.setActionCommand("ok");
      this.okButton.setPreferredSize(new Dimension(80, 30));
      this.okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          String id = AccountPasswordEnterWindow.this.accountTextField.getText();
          if ((id == null) || (id.trim().length() < 1)) {
            AccountPasswordEnterWindow.this.errorLabel.setText("Account is empty!");
            return;
          }
//          AccountPasswordEnterWindow.access$402(AccountPasswordEnterWindow.this, id);
//          char[] pw = AccountPasswordEnterWindow.this.passwordPasswordField.getPassword();
//          if ((pw == null) || (pw.length == 0)) AccountPasswordEnterWindow.access$502(AccountPasswordEnterWindow.this, "");
//          else AccountPasswordEnterWindow.access$502(AccountPasswordEnterWindow.this, new String(pw));
//          AccountPasswordEnterWindow.access$602(AccountPasswordEnterWindow.this, true);
          AccountPasswordEnterWindow.this.setVisible(false);
        }
      });
    }

    return this.okButton;
  }

  private JButton getCancelButton() {
    if (this.cancelButton == null) {
      this.cancelButton = new JButton("Cancel");
      this.cancelButton.setActionCommand("cancel");
      this.cancelButton.setPreferredSize(new Dimension(80, 30));
      this.cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          AccountPasswordEnterWindow.this.setAccount(null);
          AccountPasswordEnterWindow.this.setPassword(null);
//          AccountPasswordEnterWindow.access$602(AccountPasswordEnterWindow.this, false);
          AccountPasswordEnterWindow.this.setVisible(false);
        }
      });
    }
    return this.cancelButton;
  }

  public boolean enterAccountAndPassword()
  {
    setVisible(true);
    return this.enterOk;
  }

  public String getAccount()
  {
    return this.account;
  }

  public void setAccount(String account)
  {
    this.account = account;
    getAccountTextField().setText(account);
  }

  public String getPassword()
  {
    return this.password;
  }

  public void setPassword(String password)
  {
    this.password = password;
    getPasswordPasswordField().setText(password);
  }
}